// pages/TakeList/index.js

import {
  HTTP
} from '../../../utils/http.js'
let Myhttp = new HTTP()


Page({

  /**
   * 页面的初始数据
   */
  data: {
    naviIndex: 0,
    returnChecked: [],
    page: '1',
    questionnaire: [],
    navi: ['发布上线', '已完成'],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let typeNum = (this.data.naviIndex + 1).toString()
    let that = this
    Myhttp.request({
      url: 'questionnaire/questionnaire/myQuestionnaireList',
      data: {
        type: typeNum,
        page: this.data.page
      },
      success: (res) => {
        // console.log('a')
        //  console.log(res)
        let datas = res.data.data
        datas.forEach((val) => {
          val.my_del = 1
          val.index = 1,
            val.checked = false
          if (this.data.naviIndex == 1) {
            val.over = '1'
          } else {
            val.over = '0'
          }
        })
        that.setData({
          questionnaire: datas
        })
      }
    })
    //console.log(this.data)
  },
  naviClick: function(e) {
    this.setData({
      naviIndex: e.currentTarget.dataset.index
    })
    let typeNum = (this.data.naviIndex + 1).toString()
    let that = this
    Myhttp.request({
      url: 'questionnaire/questionnaire/myQuestionnaireList',
      data: {
        type: typeNum,
        page: this.data.page
      },
      success: (res) => {
        let datas = res.data.data
        datas.forEach((val) => {
          if (typeNum == '1') {
            val.my_del = 1
            val.index = 1
            val.checked = false
            if (that.data.naviIndex == 1) {
              val.over = '1'
            } else {
              val.over = '0'
            }
          } else {
            val.checked = false
            val.index = 1
            if (that.data.naviIndex == 1) {
              val.over = '1'
            } else {
              val.over = '0'
            }
          }

          // console.log(this.data.naviIndex)
          if (this.data.naviIndex == 1) {
            val.over = '1'
          } else {
            val.over = '0'
          }
        })
        that.setData({
          questionnaire: datas
        })
        console.log(this.data.questionnaire)
      }
    })

  },
  Establish: function() {
    Myhttp.jump({
      url: '/pages/PublishQuestionnaire/PublishProblem/index',
    })
    // wx.navigateTo({
    //   url: '/pages/PublishQuestionnaire/PublishProblem/index',
    // })
  },
  Edit: function() {
    if (this.data.questionnaire.length > 0) {
      for (let i = 0; i < this.data.questionnaire.length; i++) {
        var index = "questionnaire[" + i + "].radioshow";
        this.setData({
          [index]: true //用[]表示的变量
        });
      }
    } else {
      wx.showToast({
        title: '暂无数据'
      })
    }

    // console.log(this.data);
  },
  returnChecked: function(e) {
    //console.log(e.detail.behavior[0].id)
    let aa = e.detail.aa
    // wx.navigateTo({
    //   url: `/pages/EditFinal/index?qid=${id}`,
    // })
    this.setData({
      returnChecked: aa
    })
  },
  deletes: function() {
    let typeNum = (this.data.naviIndex + 1).toString()
    let that = this
    let url = 'questionnaire/questionnaire/delQuestionnaire'
    Myhttp.request({
      url: url,
      data: {
        qids: this.data.returnChecked
      },
      success: (res) => {
        Myhttp.request({
          url: 'questionnaire/questionnaire/myQuestionnaireList',
          data: {
            type: typeNum,
            page: this.data.page
          },
          success: (res) => {
            that.setData({
              questionnaire: res.data.data
            })
          }
        })
      }
    })
    // console.log(this.data.reward[i], "reward[" + i + "]");
  },
  my_del: function(e) {
    // url = 'questionnaire/questionnaire/endQuestionnaire'
    // console.log(e.currentTarget.dataset.index.id)
    // console.log(e)
    let id = e.detail.index.id

    let typeNum = (this.data.naviIndex + 1).toString()
    let that = this
    let url = 'questionnaire/questionnaire/endQuestionnaire'

    Myhttp.request({
      url: url,
      data: {
        id: id
      },
      success: (res) => {
        Myhttp.request({
          url: 'questionnaire/questionnaire/myQuestionnaireList',
          data: {
            type: typeNum,
            page: this.data.page
          },
          success: (res) => {
            let datas = res.data.data
            datas.forEach((val) => {
              val.index = 1,
                val.my_del = 1
              if (this.data.naviIndex == 1) {
                val.over = '1'
              } else {
                val.over = '0'
              }
              val.checked = false
            })
            that.setData({
              questionnaire: datas
            })
          }
        })
      }
    })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    // this.setData({
    //   returnChecked: [],
    //   page: '1',
    //   questionnaire: [],
    // })
    // this.onLoad()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    // console.log('卸载了index')
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.setData({
      returnChecked: [],
      page: '1',
      questionnaire: [],
    })

    let typeNum = (this.data.naviIndex + 1).toString()
    let that = this
    Myhttp.request({
      url: 'questionnaire/questionnaire/myQuestionnaireList',
      data: {
        type: typeNum,
        page: this.data.page
      },
      success: (res) => {
        let datas = res.data.data
        datas.forEach((val) => {
          val.index = 1,
            val.my_del = 1
          if (this.data.naviIndex == 1) {
            val.over = '1'
          } else {
            val.over = '0'
          }
          val.checked = false
        })
        that.setData({
          questionnaire: datas
        })
        wx.stopPullDownRefresh() //停止下拉刷新
      }
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    let typeNum = (this.data.naviIndex + 1).toString()
    let page = this.data.page
    page++
    this.setData({
      page: page
    })
    let that = this
    Myhttp.request({
      url: 'questionnaire/questionnaire/myQuestionnaireList',
      data: {
        type: typeNum,
        page: this.data.page
      },
      success: (res) => {
        let data = this.data.questionnaire
        let datas = res.data.data
        if (datas.length > 0) {
          datas.forEach((val) => {
            val.index = 1,
              val.checked = false
            if (this.data.naviIndex == 1) {
              val.over = '1'
            } else {
              val.my_del = 1
              val.over = '0'
            }
          })
          datas.forEach(val => {
            data.push(val)
          })
          that.setData({
            questionnaire: data
          })
        }

      }
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    return {
      path: '/pages/personal/Mypublish/index'
    }
  }
})